1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::iter::FromIterator;
use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StringSet {
inner: Arc<[SmolStr]>,
}
impl<S> FromIterator<S> for StringSet
where
S: Into<SmolStr>,
{
fn from_iter<T: IntoIterator<Item = S>>(iter: T) -> Self {
let mut buf: Vec<SmolStr> = iter.into_iter().map(Into::into).collect();
buf.sort_unstable();
buf.dedup();
StringSet { inner: buf.into() }
}
}
impl StringSet {
#[inline]
pub fn singleton(s: impl Into<SmolStr>) -> StringSet {
StringSet {
inner: Arc::from([s.into()]),
}
}
#[inline]
pub fn contains(&self, s: &str) -> bool {
self.inner.binary_search_by_key(&s, |x| &**x).is_ok()
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn set_cmp(&self, other: &StringSet) -> Option<Ordering> {
match self.len().cmp(&other.len()) {
Greater if other.is_subset(self) => Some(Greater),
Equal if self == other => Some(Equal),
Less if self.is_subset(other) => Some(Less),
_ => None,
}
}
#[inline]
pub fn is_subset(&self, other: &StringSet) -> bool {
if self.len() > other.len() {
return false;
}
let mut other = &other.inner[..];
for s in self.iter() {
if let Ok(ix) = other.binary_search_by_key(&s, |x| &**x) {
other = &other[(ix + 1)..];
} else {
return false;
}
}
true
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &str> {
self.inner.iter().map(|s| &**s)
}
}
impl Index<usize> for StringSet {
type Output = str;
fn index(&self, index: usize) -> &Self::Output {
&self.inner[index]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic_string_set_properties() {
let a: StringSet = ["x", "y", "z", "x"].iter().map(SmolStr::new).collect();
assert_eq!(a.len(), 3);
assert!(a.is_subset(&a));
assert_eq!(a.set_cmp(&a), Some(Equal));
assert!(a.contains("x"));
assert!(!a.contains("w"));
let b: StringSet = ["x", "y", "z", "t", "t", "z", "x"]
.iter()
.map(SmolStr::new)
.collect();
assert_eq!(b.len(), 4);
assert_ne!(a, b);
assert_eq!(b.set_cmp(&b), Some(Equal));
assert_eq!(a.set_cmp(&b), Some(Less));
assert_eq!(b.set_cmp(&a), Some(Greater));
assert!(a.is_subset(&b));
assert!(!b.is_subset(&a));
}
}